package com.wgchao.amc.controller.autho;

import com.wgchao.amc.common.BindingError;
import com.wgchao.amc.common.MenuAutho;
import com.wgchao.amc.common.ResultCommon;
import com.wgchao.amc.common.validate.Save;
import com.wgchao.amc.mapstruct.dto.SysAuthoUserDTO;
import com.wgchao.amc.service.UserAuthoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: UserAuthoController
 * @author: wgchao
 * @createTime: 2019/10/31 10:43 AM
 * @Description: 用户权限管理
 */
@RestController
@RequestMapping("/amc/sys/autho/user")
@Api(tags = "用户菜单权限管理")
public class UserAuthoController {
    @Autowired
    private UserAuthoService userAuthoService;

    @PostMapping("/assign/{userId}/{proMark}")
    @ApiOperation(value = "为用户分配菜单权限")
    @MenuAutho(desc = "为用户分配菜单权限")
    @ApiParam(name = "authoUserDTOS", value = "菜单权限信息", required = true)
    public ResultCommon<List<SysAuthoUserDTO>> assign(@PathVariable Long userId,
                                                      @PathVariable String proMark,
                                                      @RequestBody @Validated(value = {Save.class}) List<SysAuthoUserDTO> authoUserDTOS,
                                                      BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return userAuthoService.assign(userId, proMark, authoUserDTOS);
    }


    @GetMapping("/search/{userId}/{proMark}")
    @ApiOperation(value = "查询用户菜单权限")
    @MenuAutho(desc = "查询用户菜单权限")
    public ResultCommon<List<SysAuthoUserDTO>> search(@PathVariable Long userId,
                                                       @PathVariable String proMark) throws Exception{
        return userAuthoService.search(userId, proMark);
    }

}
